package com.credit.mola.app.utils.helper

import android.annotation.SuppressLint
import com.credit.mola.app.utils.config.Configs
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date

/**
 * ProcessHelper
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:44
 * @Annotation：用户主流程状态 帮助类
 */
object SikadwaMainProcessHelper {

    /**
     * 保存用户流程数据
     */
    fun saveSikadwaMainProcessData(data: JSONObject){
        Configs.saveSPString(Configs.SPKEY.SIKADWA_PROCESS, data.toString())
    }

    /**
     * 获取流程数据
     */
    fun takeSikadwaMainProcessData(): JSONObject? {
        val dataString = Configs.getSPString(Configs.SPKEY.SIKADWA_PROCESS)
        if (dataString.isEmpty()){
            return null
        }
        return JSONObject(dataString)
    }

    /**
     * 获取所有流程titleList
     */
    fun takeSikadwaMainProcessAllStepTitleList(): ArrayList<String> {
        val titleList = arrayListOf<String>()
        val data = takeSikadwaMainProcessData() ?: return titleList

        val allStepTitles = data.optJSONArray("allStepTitles")
        if (allStepTitles != null) {
            for (i in 0..<allStepTitles.length()){
                titleList.add(allStepTitles.optString(i))
            }
        }
        return titleList
    }

    /**
     * 获取总流程步骤key
     */
    fun takeSikadwaMainProcessAllStepList(): ArrayList<String>{
        val allStepList = arrayListOf<String>()
        val data = takeSikadwaMainProcessData() ?: return allStepList
        val allSteps = data.optJSONArray("allSteps")
        if (allSteps != null){
            for (i in 0..<allSteps.length()){
                allStepList.add(allSteps.optString(i))
            }
        }
        return allStepList
    }

    /**
     * 获取剩余未完成的流程列表
     */
    fun takeSikadwaMainProcessStepList(): ArrayList<String>{
        val stepList = arrayListOf<String>()
        val data = takeSikadwaMainProcessData() ?: return stepList
        val steps = data.optJSONArray("steps")
        if (steps != null){
            for (i in 0..<steps.length()){
                stepList.add(steps.optString(i))
            }
        }
        return stepList
    }

    /**
     * 获取获取当前进行的步骤下标
     */
    fun takeSikadwaMainProcessStepIndex(): Int{
        var stepIndex = takeSikadwaMainProcessAllStepList().size
        if (takeSikadwaMainProcessStepList().size == 0){
            //所有步骤已完成
            return stepIndex
        }
        val curtStep = takeSikadwaMainProcessStepList()[0]
        for (i in 0..<stepIndex){
            if (curtStep == takeSikadwaMainProcessAllStepList()[i]){
                stepIndex = i
                break
            }
        }
        return stepIndex
    }

    /**
     * 获取订单状态
     */
    fun takeSikadwaMainProcessOrderStatus(): String{
        val data = takeSikadwaMainProcessData() ?: return ""
        return data.optString("orderStatus","")
    }

    /**
     * 获取申请状态
     */
    fun takeSikadwaMainProcessApplyStatus(): String{
        val data = takeSikadwaMainProcessData() ?: return ""
        return data.optString("applyStatus","")
    }

    /**
     * 授信状态
     */
    fun takeSikadwaMainProcessBeforeRiskStatus(): String{
        val data = takeSikadwaMainProcessData() ?: return ""
        return data.optString("beforeRiskStatus","")
    }

    /**
     * 是否是可以修改手机号
     */
    fun isSikadwaMainAuthMobileEditFlag(): Boolean{
        val data = takeSikadwaMainProcessData() ?: return false
        return data.optString("mobileEditFlag") == "YES"
    }

    /**
     * 是否开放WhatsApp手机校验方式  YES、NO
     */
    fun isSikadwaMainAuthCheckWhatsApp(): Boolean{
        val data = takeSikadwaMainProcessData() ?: return false
        return data.optString("whatsappFlag") == "YES"
    }

    /**
     * 是否开放SMS 手机校验方式  YES、NO
     */
    fun isSikadwaMainAuthCheckSMS(): Boolean{
        val data = takeSikadwaMainProcessData() ?: return false
        return data.optString("smsFlag") == "YES"
    }

    /**
     * 是否需要校验手机号
     *  whatsappFlag=”YES“ || smsFlag=”YES“
     */
    fun isSikadwaMainAuthCheckMobile(): Boolean{
        if (isSikadwaMainAuthCheckSMS() ||
            isSikadwaMainAuthCheckWhatsApp()){
            return true
        }
        return false
    }

    /**
     * 获取可再次申请时间
     */
    fun takeSikadwaMainProcessApplyAgainDate(): String{
        val data = takeSikadwaMainProcessData() ?: return ""
        return data.optString("againApplyDate")
    }

    /**
     * 获取授信到期时间
     */
    fun takeSikadwaMainProcessCreditExpireDate(): String{
        val data = takeSikadwaMainProcessData() ?: return ""
        if (!data.has("creditExpireDate")){
            val calendar = Calendar.getInstance()
            calendar.add(Calendar.YEAR,1)
            return getFormatTime(calendar.timeInMillis,"MM/dd/yyyy")
        }
        return data.optString("creditExpireDate")
    }

    /**
     * 格式化时间
     */
    @SuppressLint("SimpleDateFormat")
    fun getFormatTime(timestamp: Long, pattern: String?): String {
        val simpleFormat = SimpleDateFormat(pattern)
        val date = Date(timestamp)
        return simpleFormat.format(date)
    }
}